﻿using System;
using System.Linq;
using eAtlantida.Tools.Generator;
using eAtlantida.Tools.Generator.Sql;

namespace eAtlantida.Tools.Operations.GenerateOperations
{
    public class GenerateClientOperation : Operation
    {
        public const string Name = "generate client";

        public override void Execute(State state)
        {
            var name = state.CommandParameters[0];
            var parameters = GetGenerateParameters(state.CommandParameters.Skip(1));

            WriteMessage("Generating client files (models, controllers, views)...", MessageType.Default);

            var discover = new Discover(state.ConnectionString);
            var generator = new ClientGenerator(
                discover.GetColumns(name), name, "Admin", parameters.IsCreate, parameters.IsUpdate, parameters.IsDelete, parameters.IsListing);

            var path = "Web/Areas/Admin/js/" + name;

            try
            {
                AddFile(path, SingleName(name.ToLowerFirstChar()) + "Routes", generator.GenerateRoutes(), ".coffee");
                WriteMessage("Added route", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage("Not added route: " + ex.Message, MessageType.Error);
            }

            if (parameters.IsCreate)
            {
                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "CreateModel", generator.GenerateCreateModel(), ".coffee");
                    WriteMessage("Added 'CreateModel'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'CreateModel': " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "CreateView", generator.GenerateCreateView(), ".coffee");
                    WriteMessage("Added 'CreateView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'CreateView': " + ex.Message, MessageType.Error);
                }
            }

            if (parameters.IsUpdate)
            {
                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "EditModel", generator.GenerateEditModel(), ".coffee");
                    WriteMessage("Added 'EditModel'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'EditModel': " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "EditView", generator.GenerateEditView(), ".coffee");
                    WriteMessage("Added 'EditView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'EditView': " + ex.Message, MessageType.Error);
                }
            }

            if (parameters.IsListing)
            {
                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "ListingModel", generator.GenerateListingModel(), ".coffee");
                    WriteMessage("Added 'ListingModel'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'ListingModel': " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(path, SingleName(name.ToLowerFirstChar()) + "ListingView", generator.GenerateListingView(), ".coffee");
                    WriteMessage("Added 'ListingView'", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added 'ListingView': " + ex.Message, MessageType.Error);
                }
            }
        }
    }
}